<?php
	require_once(dirname(__FILE__).'/appconf.php');

	class PieChart extends Site{

		function setup(){
			$this->run_modes('test','getImgUrl');
			$this->start_mode('test');
			$this->selfurl = '/pie_chart.php';
			parent::setup();
		}

		function cgiapp_prerun($rm=''){
			parent::cgiapp_prerun();
		}

		function action_getImgUrl($data = array()){
			/* $data = array(
				'type' => 'p',	//圖表類型為2D餅圖
				'width' => '130',	//圖片寬度
				'height' => '130',	//圖片高度
				'datas' => array(1,2,3,4,5),	//圖片數據
				'colors' => array('B31B54','C91B60','E84F6D','E4757E','ECA7B9'),	//圖片分欄顏色
				'bgcolor' => 'FFFFFF'	//圖片背景顏色
			) */
			$_type = isset($data['type'])&&!empty($data['type'])?$data['type']:'p';	//圖表類型為2D餅圖
			$_width = isset($data['width'])&&!empty($data['width'])?$data['width']:'130';	//圖片寬度
			$_height = isset($data['height'])&&!empty($data['height'])?$data['height']:'130';	//圖片高度
			
			$data_null = 0;
			foreach($data['datas'] as $k=>$v){
				$data_null += $v;
			}
			if($data_null<=0){$data['datas'][count($data['datas'])-1] = 1;}
			
			$_data = isset($data['datas'])&&!empty($data['datas'])?implode(',',$data['datas']):implode(',',array(1,2,3,4,5));	//圖片數據
			$_color = isset($data['colors'])&&!empty($data['colors'])?implode('|',$data['colors']):implode('|',array('B31B54','C91B60','E84F6D','E4757E','ECA7B9'));	//圖片分欄顏色
			$_bgcolor = isset($data['bgcolor'])&&!empty($data['bgcolor'])?$data['bgcolor']:'FFFFFF';	//圖片背景顏色
			
			$ImgUrl = "http://chart.apis.google.com/chart?cht=".$_type."&chs=".$_width."x".$_height."&chd=t:".$_data."&chf=bg,s,".$_bgcolor."&chco=".$_color;
			return $ImgUrl;
		}
		
		function action_test(){
			echo "<img src='".$this->action_getImgUrl(array('datas'=>array(0,1,0,1,0)))."' />";
		}
		
		
	}
	$webapp = new PieChart();
	$webapp->run();
?>